package com.galudisu.handsonic.modules.system.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.galudisu.handsonic.base.AudiTableEntity;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Barudisshu
 */
@Entity
@Table(name = "t_sys_user")
public class User extends AudiTableEntity {

    private static final long serialVersionUID = 6898375010877174045L;

    @Basic
    @Column(name = "username", nullable = true, unique = true, length = 100)
    private String username;
    @Basic
    @Column(name = "password", nullable = true, length = 100)
    private String password;
    @Basic
    @Column(name = "salt", nullable = true, length = 100)
    private String salt;
    @Basic
    @Column(name = "locked", nullable = true)
    private Boolean locked;


    @JsonIgnore
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "t_sys_user_organization",
            joinColumns = {@JoinColumn(name = "user_id", nullable = false, updatable = false)},
            inverseJoinColumns = {@JoinColumn(name = "organization_id", nullable = false, updatable = false)})
    private Set<Organization> organizations = new HashSet<>();

    @JsonIgnore
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "t_sys_user_role",
            joinColumns = {@JoinColumn(name = "user_id", nullable = false, updatable = false)},
            inverseJoinColumns = {@JoinColumn(name = "role_id", nullable = false, updatable = false)})
    private Set<Role> roles = new HashSet<>();


    public User() {
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    @JsonIgnore
    @Transient
    public String getCredentialsSalt() {
        return username + salt;
    }

    // getter setter


    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public Boolean getLocked() {
        return locked;
    }

    public void setLocked(Boolean locked) {
        this.locked = locked;
    }

    public Set<Organization> getOrganizations() {
        return organizations;
    }

    public void setOrganizations(Set<Organization> organizations) {
        this.organizations = organizations;
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }
}
